package com.ty.admin.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ty.common.constant.ResultMsgEnum;
import com.ty.common.validator.group.SaveGroup;
import com.ty.common.validator.group.UpdateGroup;
import com.ty.skysystem.entity.CarriersEntity;
import com.ty.skysystem.entity.TruckEntity;
import com.ty.skysystem.entity.req.TruckEntityREQ;
import com.ty.skysystem.service.CarriersService;
import com.ty.skysystem.service.TruckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.ty.skysystem.entity.SchedulingEntity;
import com.ty.skysystem.service.SchedulingService;
import com.ty.common.utils.PageUtils;
import com.ty.common.utils.R;

/**
 * 调度作业信息表: 用于存放和管理调度作业信息 字段名: carriers_id => 对应表: carriers 字段名: truck_id => 对应表: truck 字段名:
 * user_id=> 对应表: user
 *
 * @author tianyuzhu
 * @email 1659770218@QQ.COM
 * @date 2020-12-11 16:01:00
 */
@RestController
@RequestMapping("scheduling")
@Api(tags = "调度任务管理模块 用于存放和管理调度作业信息")
public class SchedulingController {
  @Autowired private SchedulingService schedulingService;
  @Autowired private CarriersService carriersService;
  @Autowired private TruckService truckService;

  /** 列表 */
  @GetMapping("/list")
  @ApiOperation(value = "查询所有的调度信息", notes = "查询所有的调度信息", response = SchedulingEntity.class)
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = schedulingService.queryPage(params);

    return R.ok().put(page);
  }

  /** 列表 Userid 换成 username */
  @GetMapping("/listScheduling")
  @ApiOperation(value = "查询所有的调度信息, 显示调度员姓名", notes = "查询所有的调度信息, 显示调度员姓名", response = SchedulingEntity.class)
  public R listScheduling(
          @RequestParam(value = "page", defaultValue = "1") Integer page,
          @RequestParam(value = "limit", defaultValue = "5") Integer limit,
          SchedulingEntity schedulingEntity
  ) {
    PageUtils pageUtils = schedulingService.queryPageScheduling(page, limit, schedulingEntity);

    return R.ok().put(pageUtils);
  }


  /** 信息 */
  @GetMapping("/info/{schedulingId}")
  @ApiOperation(value = "查询单个调度信息", notes = "查询单个调度信息", response = SchedulingEntity.class)
  public R info(@PathVariable("schedulingId") Long schedulingId) {
    QueryWrapper<SchedulingEntity> queryWrapper = new QueryWrapper<>();
    queryWrapper
        .select(SchedulingEntity.class, e -> !"is_deleted".equals(e.getColumn()))
        .lambda()
        .eq(SchedulingEntity::getSchedulingId, schedulingId);
    SchedulingEntity scheduling = schedulingService.getOne(queryWrapper);

    return R.ok().put(scheduling);
  }

  /** 保存 */
  @PostMapping("/save")
  @ApiOperation(value = "添加调度信息", notes = "添加调度信息")
  public R save(@RequestBody @Validated(SaveGroup.class) SchedulingEntity scheduling) {

    return schedulingService.save(scheduling)
        ? R.ok(ResultMsgEnum.INSERT_OK.getMsg())
        : R.error(ResultMsgEnum.INSERT_ERROR.getMsg());
  }

  /** 修改 */
  @PatchMapping("/update")
  @ApiOperation(value = "修改调度信息", notes = "修改调度信息")
  public R update(@RequestBody @Validated(UpdateGroup.class) SchedulingEntity scheduling) {

    return schedulingService.updateById(scheduling)
        ? R.ok(ResultMsgEnum.UPDATE_OK.getMsg())
        : R.error(ResultMsgEnum.UPDATE_ERROR.getMsg());
  }

  /** 删除 */
  @DeleteMapping("/delete")
  @ApiOperation(value = "删除调度信息", notes = "删除调度信息")
  public R delete(@RequestBody Long[] schedulingIds) {
    if (schedulingIds.length == 0) return R.error("ID不能为空");

    return schedulingService.removeByIds(Arrays.asList(schedulingIds))
        ? R.ok(ResultMsgEnum.DELETE_OK.getMsg())
        : R.error(ResultMsgEnum.DELETE_ERROR.getMsg());
  }

  /** 查询出未调度承运单 */
  @GetMapping("/unscheduledList")
  @ApiOperation(value = "查询全部未调度承运信息", notes = "查询全部未调度承运信息", response = CarriersEntity.class)
  public R unscheduledList(@RequestParam Map<String, Object> params) {
    PageUtils page = carriersService.queryPageUnscheduledCarriers(params);

    return R.ok().put(page);
  }
  /** 未参与调度的已绑定驾驶员的车辆 */
  @GetMapping("unscheduledDispatchBindDriverTruck")
  @ApiOperation(value = "未参与调度的已绑定驾驶员的车辆", notes = "未参与调度的已绑定驾驶员的车辆", response = TruckEntity.class)
  public R UnscheduledDispatchBindDriverTruck(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "limit", defaultValue = "5") Integer limit,
      TruckEntityREQ truckEntityREQ) {
    PageUtils pageUtils =
        truckService.unscheduledDispatchBindDriverTruck(page, limit, truckEntityREQ);
    return R.ok().put(pageUtils);
  }

  /** 将现有未调度承运单任务分配给现有未参与调度的已绑定驾驶员的车辆 */
  @PostMapping("distribution")
  @ApiOperation(
      value = "将现有未调度承运单任务分配给现有未参与调度的已绑定驾驶员的车辆",
      notes = "将现有未调度承运单任务分配给现有未参与调度的已绑定驾驶员的车辆")
  public R distribution(
      @RequestBody @Validated(SaveGroup.class) SchedulingEntity schedulingEntity) {

    return this.schedulingService.saveScheduling(schedulingEntity) ? R.ok("调度成功") : R.error("调度失败");
  }
}
